package cn.tedu.charging.order.dao.repository.impl;

import cn.tedu.charging.order.constant.OrderStatusConstant;
import cn.tedu.charging.order.dao.mapper.OrderFailMapper;
import cn.tedu.charging.order.dao.mapper.OrderSuccessMapper;
import cn.tedu.charging.order.dao.repository.OrderRepository;
import cn.tedu.charging.order.pojo.po.ChargingBillFailPO;
import cn.tedu.charging.order.pojo.po.ChargingBillSuccessPO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

//@Repository //记得加spring注解进入容器，Repository是持久层（DAO层）的意思
//@Repository("orderRepository") 指定名称 方便 SpringContextJobUtils通过名称来获取bean对象
@Repository("orderRepository")  //9.2 给OrderRepositoryImpl在容器里的bean对象指定了名字
public class OrderRepositoryImpl implements OrderRepository {

    @Autowired
    private OrderSuccessMapper orderSuccessMapper;

    @Autowired
    private OrderFailMapper orderFailMapper;

    @Override
    public void saveSuccessOrder(ChargingBillSuccessPO chargingBillSuccessPO) {
        orderSuccessMapper.insert(chargingBillSuccessPO);
    }

    @Override
    public void saveFailOrder(ChargingBillFailPO chargingBillFailPO) {
        orderFailMapper.insert(chargingBillFailPO);
    }

    @Override   //里面的2是程序员经常偷懒替代 To 。。。
    public void updateOrderStatusFromProcess2ExceptionEnd(String orderNo) {
        //把正在充电中的订单数据的状态修改为 异常结束
        //update charging_bill_success set bill_status = 3 where bill_id = orderNo and bill_status = 1

        // set更新的目标值   详细使用见黑马
        // PO类里对应数据表的订单状态BillStatus设为3，mybatis-plus执行时，只会更新非空值，也就是只把订单状态 1充电中 改为 3异常结束
        ChargingBillSuccessPO chargingBillSuccessPO = new ChargingBillSuccessPO();
        chargingBillSuccessPO.setBillStatus(OrderStatusConstant.ORDER_STATUS_EXCEPTION_END);
        // 更新的where条件   UpdateWrapper是mybatis-plus中，专门用于在数据层外写sql条件的，具体见黑马      傻逼侯亚强就一句这是设置条件的，就没了！！！
        UpdateWrapper<ChargingBillSuccessPO> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("bill_status", OrderStatusConstant.ORDER_STATUS_PROGRESS);
        //updateWrapper.eq("bill_id",orderMQPO.getOrderNo());
        updateWrapper.eq("bill_id",orderNo);    //8.5数据层解耦后，直接传参就是订单编号

        //用mybatis-plus，查看update代码，第一个参数为entity，是set的值，第二个参数为where条件，所以上面写参数
        orderSuccessMapper.update(chargingBillSuccessPO,updateWrapper);
    }

    @Override   //9.2 自检任务里，判断订单成功/失败表有没有数据     DeviceCheckJob需要
    public ChargingBillSuccessPO getSuccessOrder(String orderNo) {
        QueryWrapper<ChargingBillSuccessPO> queryWrapper = new QueryWrapper();  //mybatis-plus写法，简单CRUD
        queryWrapper.eq("bill_id",orderNo);
        return orderSuccessMapper.selectOne(queryWrapper);
    }
    @Override
    public ChargingBillFailPO getFailOrder(String orderNo) {
        QueryWrapper<ChargingBillFailPO> queryWrapper = new QueryWrapper();
        queryWrapper.eq("bill_id",orderNo);
        return orderFailMapper.selectOne(queryWrapper);
    }

    @Override   //9.3增加
    public void updateOrderDeviceInfo(String orderNo, int gunId) {
        //UpdateWrapper 更新的条件
        UpdateWrapper<ChargingBillSuccessPO> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("bill_id",orderNo);
        // 更新的目标值
        ChargingBillSuccessPO chargingBillSuccessPO = new ChargingBillSuccessPO();
        chargingBillSuccessPO.setGunId(gunId);

        orderSuccessMapper.update(chargingBillSuccessPO,updateWrapper);
    }
}
